//
//  PlayViewModel+Map.swift
//  ImGuider X
//
//  Created by 陆涛 on 2018/10/26.
//  Copyright © 2018 imguider. All rights reserved.
//

import UIKit
//import JZLocationConverterSwift

extension PlayViewModel {
    
    private struct PlayViewModelKey {
        
        static var deawLineTimerKey = "deawLineTimerKey"
        static var autoplayTimerKey = "autoplayTimerKey"
        static var hasDrawLineKey = "hasDrawLineKey"
        static var annsKey = "annsKey"
        static var alertTimeIntervalKey = "alertTimeIntervalKey"
    }
    
    var anns:[TripAnnotion]? {
        
        get {
            guard let anns = objc_getAssociatedObject(self, &PlayViewModelKey.annsKey) as? [TripAnnotion] else {
                return nil
            }
            return anns
        }
        set {
            objc_setAssociatedObject(self, &PlayViewModelKey.annsKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
        
    }
    
    fileprivate var deawLineTimer:Timer? {
        
        get {
            guard let timer = objc_getAssociatedObject(self, &PlayViewModelKey.deawLineTimerKey) as? Timer else {
                return nil
            }
            return timer
        }
        set {
            objc_setAssociatedObject(self, &PlayViewModelKey.deawLineTimerKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    
    fileprivate var autoplayTimer:Timer? {
        
        get {
            guard let timer = objc_getAssociatedObject(self, &PlayViewModelKey.autoplayTimerKey) as? Timer else {
                return nil
            }
            return timer
        }
        set {
            objc_setAssociatedObject(self, &PlayViewModelKey.autoplayTimerKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    
    var hasDrawLine:Bool {
        
        get {
            guard let hasDraw = objc_getAssociatedObject(self, &PlayViewModelKey.hasDrawLineKey) as? Bool else {
                return false
            }
            return hasDraw
        }
        set {
            objc_setAssociatedObject(self, &PlayViewModelKey.hasDrawLineKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    
    var alertTimeInterval:TimeInterval {
        
        get {
            guard let interval = objc_getAssociatedObject(self, &PlayViewModelKey.alertTimeIntervalKey) as? TimeInterval else {
                return 0
            }
            return interval
        }
        set {
            objc_setAssociatedObject(self, &PlayViewModelKey.alertTimeIntervalKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    
    func statrtDrawTimer() {
        
        stopDrawTimer()
        self.deawLineTimer = Timer.scheduledTimer(timeInterval: 30, target: self, selector: #selector(deawLineTimerAction), userInfo: nil, repeats: true)
    }
    
    func statrtAutoplayTimer() {
        
        stopAutoplayTimer()
        
        self.autoplayTimer = Timer.scheduledTimer(timeInterval: 10, target: self, selector: #selector(autoplayAction), userInfo: nil, repeats: true)
    }
    
    func stopDrawTimer() {
        
        self.deawLineTimer?.invalidate()
        self.deawLineTimer = nil
    }
    
    func stopAutoplayTimer() {
        
        self.autoplayTimer?.invalidate()
        self.autoplayTimer = nil
    }
    
    @objc func deawLineTimerAction() {
        
        if self.hasDrawLine {
            return
        }
        
        self.mapDrawLineAction?()
    }
    
    @objc func autoplayAction() {
        
        if !AppManager.share.automaticPlay {
            
            return
        }
        
        guard let anns = anns, let coorSelf = LocationManager.share.gcj02 else {
            return
        }
        
        
        for ann in anns  {
            
            guard let record = ann.model as? Record,
                let autoplay = record.autoplay,autoplay == 1,
                let range = record.autoplayrange,range > 1 else { continue }
            
            let distance = LocationManager.distance(form: coorSelf, to: ann.coordinate)
            
            if distance > Double(range) {
                
                continue
            }
            
            ann.hasNav = true
            
            if WPY_AVPlayer.playManager.isPlay {
                
                let now = Date().timeIntervalSince1970
                
                let minsDifferenr = now - self.alertTimeInterval
                
                if minsDifferenr <= 180 {
                    
                    continue
                }
                
                self.alertTimeInterval = now
                
                self.autoPlayAction?(ann,true)
                
            } else {
                
                self.alertTimeInterval = 0
                self.autoPlayAction?(ann,false)
            }
            
            return
            
        }
    }
}

extension PlayViewModel {
    
    func reloadMapdata() {

        self.anns = records?.compactMap({ record -> TripAnnotion? in
            
            if let coor = record.coor {
                
                let ann = TripAnnotion(coor: coor)
                ann.model = record
                
                return ann
            }
            
            return nil
        })
        
        self.reloadMapFinished?()
    }
}


